﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameUtils.Input;

namespace GameControls
{
    public sealed class ScrollView : ControlBase
    {
        #region Constants
        public const int SLIDER_WIDTH = 12;
        #endregion

        #region Properties
        private Slider mSlider;
        #endregion

        #region Constructors
        public ScrollView(Rectangle pRect, Texture2D pSliderTexture)
            : base(pRect)
        {
            Rectangle sliderRect = new Rectangle(
                pRect.Width - SLIDER_WIDTH,
                0,
                SLIDER_WIDTH,
                pRect.Height);
            mSlider = new Slider(sliderRect, pSliderTexture);
            AddComponent(mSlider);
            UpdateSlider();
            mSlider.KnobPositionChanged += KnobPositionChangedHandler;
        }
        #endregion

        #region Overriden Methods
        protected override void Dispose(bool pDisposing)
        {
            if (pDisposing)
            {
                mSlider.KnobPositionChanged -= KnobPositionChangedHandler;
                RemoveComponent(mSlider);
            }
        }

        protected override void UpdateImpl(GameTime pGameTime)
        {
        }

        protected override void OnChildrenChanged()
        {
            base.OnChildrenChanged();
            UpdateSlider();
        }
        #endregion

        #region Methods
        private void UpdateSlider()
        {
            Rectangle contentBoundingRectangle = GetContentBoundingRectangle();
            mSlider.KnobSize = Rect.Height;
            mSlider.SliderStartValue = contentBoundingRectangle.Y;
            mSlider.SliderRange = contentBoundingRectangle.Height;
        }
        #endregion

        #region Event Handlers
        private void KnobPositionChangedHandler(object sender, EventArgs e)
        {
            SetChildrenOffsetY((int)-mSlider.KnobPosition);
        }
        #endregion
    }
}